package com.kaiwukj.android.ufamily.mvp.ui.page.service.complain;

import androidx.annotation.NonNull;

import com.kaiwukj.android.mcas.mvp.BasePresenter;
import com.kaiwukj.android.mcas.utils.RxLifecycleUtils;
import com.kaiwukj.android.ufamily.mvp.http.callback.ApiCallBack;
import com.kaiwukj.android.ufamily.mvp.http.entity.base.ListResp;
import com.kaiwukj.android.ufamily.mvp.http.entity.params.PostComplainParams;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.ComplainResult;
import com.kaiwukj.android.ufamily.mvp.http.utils.RxUtils;
import com.kaiwukj.android.ufamily.mvp.ui.page.service.ServiceContract;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;

/**
 * @author Charles
 * @date 2019/8/14.
 * description：Kaiwu
 */

public class ComplainPresenter extends BasePresenter<ServiceContract.Model, ServiceContract.ComplainView> {

    public static final int COMMIT = 1;

    @Inject
    RxErrorHandler mErrorHandler;

    @Inject
    public ComplainPresenter(ServiceContract.Model model, ServiceContract.ComplainView rootView) {
        super(model, rootView);
    }

    /**
     * 获取七牛云Token
     */
    public void requestQIToken() {
        mModel.getQiNiuToken()
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .compose(RxUtils.schedulerHelper())
                .subscribe(new ApiCallBack<String>(mErrorHandler, mRootView) {
                    @Override
                    public void onSuccess(@NonNull String result) {
                        mRootView.onGetQiNiuToken(result);
                    }

                    @Override
                    public void onFailure(@NonNull Throwable e) {
                        super.onFailure(e);
                        mRootView.showMessage("获取七牛token失败");
                        mRootView.killMyself();
                    }
                });
    }

    /**
     * 提交投诉
     */
    public void postComplain(PostComplainParams request) {
        mModel.postComplain(request)
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .doFinally(() -> mRootView.hideLoading())
                .compose(RxUtils.schedulerHelper())
                .subscribe(new ApiCallBack<Integer>(mErrorHandler, mRootView, COMMIT) {

                    @Override
                    public void onSuccess(@NonNull Integer result) {
                        //mRootView.showMessage("提交成功");
                    }
                });
    }

    /**
     * 获取投诉列表
     */
    public void requestComplainList(int page, int pageSize) {
        mModel.fetchComplainList(page, pageSize)
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .compose(RxUtils.schedulerHelper())
                .subscribe(new ApiCallBack<ListResp<ComplainResult>>(mErrorHandler, mRootView) {
                    @Override
                    public void onSuccess(@NonNull ListResp<ComplainResult> result) {
                        mRootView.onFetchComplainList(result.getList());
                    }
                });
    }

}
